<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>正则式</title>
    <script src="/static/js/jquery-3.4.0.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {
            /*
            $('#btn_show').click(function () {
                var sql = $('#sql').val();
                var a = sql.match(/^select\s+/i);
                console.log(a);
            });
             */

            function rp(line)
            {
                if (line.match(/^--/i) != null)
                {
                    return rp($.trim(line.replace(/^--/i, '')));
                }
                return line;
            }

            $('#btn_show').click(function () {
                var sql = $('#sql').val();
                sql = sql.replace(/(?<=--)[\s\S]*?(?=\n)/g, '');
                console.log(rp(sql));
            });

            $('#btn_hide').click(function () {
                var sql = $('#sql').val();
                alert(sql);
            });

            /*
            $('#btn_comment').click(function () {
                var sql = $('#sql').val();
                sql = sql.replace(/(?<=--)[\s\S]*?(?=\n)/g, '');
                alert(sql);
            });
             */

            function first(vs) {
                if (vs.length > 0)
                {
                    return vs[0];
                }
                return null;
            }

            function rest(vs) {
                return vs.slice(1);
            }

            function eliminate_comment(vs, lst, lst_0, rs)
            {
                var f = first(vs);
                if (f != null) {
                    var r = rest(vs);
                    if (f == '-' && first(r) == '-' && lst_0.length == 0) {
                        lst.push(1);
                        eliminate_comment(rest(r), lst, lst_0, rs);
                    }
                    else if (f == '\n')
                    {
                        lst.pop();
                        eliminate_comment(r, lst, lst_0, rs);
                    }
                    else if (lst.length > 0)
                    {
                        eliminate_comment(r, lst, lst_0, rs);
                    }
                    else if (f == '/' && first(r) == '*' && lst.length == 0) {
                        lst_0.push(1);
                        eliminate_comment(rest(r), lst, lst_0, rs);
                    }
                    else if (f == '*' && first(r) == '/')
                    {
                        lst_0.pop();
                        eliminate_comment(rest(r), lst, lst_0, rs);
                    }
                    else if (lst_0.length > 0)
                    {
                        eliminate_comment(r, lst, lst_0, rs);
                    }
                    else if (lst_0.length == 0 && lst.length == 0)
                    {
                        rs.push(f);
                        eliminate_comment(r, lst, lst_0, rs);
                    }
                }

                return rs;
            }

            function my_comment(vs)
            {
                return eliminate_comment(vs, [], [], []);
            }

            function has_semicolon(vs)
            {
                var lst = [];
                var flag = null;
                for (var i = 0; i < vs.length; i++)
                {
                    console.log(vs[i]);
                    if (vs[i] == '"' && (flag == null || flag == '大'))
                    {
                        if (flag == null)
                        {
                            flag = '大';
                        }

                        if (lst.length > 0)
                        {
                            lst.pop();
                            if (lst.length == 0)
                            {
                                flag = null;
                            }
                        }
                        else
                        {
                            lst.push(i);
                        }
                    }
                    else if (vs[i] == "'" && (flag == null || flag == '小'))
                    {
                        if (flag == null)
                        {
                            flag = '小';
                        }

                        if (lst.length > 0)
                        {
                            lst.pop();
                            if (lst.length == 0)
                            {
                                flag = null;
                            }
                        }
                        else
                        {
                            lst.push(i);
                        }
                    }
                    else if (vs[i] == ';' && flag == null && i < vs.length - 1)
                    {
                        return false;
                    }
                }

                if (flag != null)
                {
                    return '字符串有错误！';
                }
                return true;
            }

            function my_single(line)
            {
                var vs = my_comment(Array.from(line));
                //var sql = vs.join('');
                return has_semicolon(vs);
            }

            $('#btn_comment').click(function () {
                var sql = $('#sql').val();
                //sql = sql.replace(/(?<=--)[\s\S]*?(?=\n)/g, '');
                //alert(sql);
                var vs = my_comment(Array.from(sql));
                console.log(vs.join(''));
            });

            $('#btn_bli').click(function () {
                var sql = $('#sql').val();
                var vs = Array.from(sql);
                console.log(vs);
                for (var i = 0; i < vs.length; i++)
                {
                    console.log(vs[i]);
                }
            });

            $('#btn_jiequ').click(function () {
                var vs = [1, 2, 3];
                vs.push(4);
                console.log(vs);
                console.log(vs.join(""));
                //console.log(vs.pop());
                //console.log(vs);
                //console.log(vs.slice(1));
            });

            $('#btn_sg').click(function () {
                var sql = $('#sql').val();
                var vs = my_single(sql);
                console.log(vs);
            });
        });
    </script>
</head>
<body>
<textarea name="sql" id="sql"
          rows="5" cols="33">
</textarea>
    <input type="button" id="btn_show" value="显示"/>
    <input type="button" id="btn_comment" value="去掉注释"/>
    <input type="button" id="btn_hide" value="隐藏"/>
    <input type="button" id="btn_bli" value="遍历"/>
    <input type="button" id="btn_jiequ" value="截取数组"/>
<input type="button" id="btn_sg" value="去掉注释后判断是否只有一条语句"/>
    <div id="dawn_sql_show1"></div>
</body>
</html>







































